package com.tceasy.gateway.api

import com.tceasy.common.utils.date.DateUtils
import com.tceasy.common.utils.json.JsonUtil
import com.tceasy.gateway.util.HttpClient
import com.tceasy.gateway.util.SignUtil
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * Created by zhanghuan on 2016/4/15.
 */
class GatewayTest {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String partner = "c83240a5ed004241912f88d8cbe35198";/*开发者账号*/
	private static final String key = "b52824346f6d4dd0ae8771fc1de31873";/*开发者密钥*/
	private static final String url = "http://test.tingjiandan.com/openapi/gateway";

	@Test
	public void testOpenAip(){
		Map map = new HashMap<>();
		map.put("service", "tcaction.luckMoney.openapi");

		map.put("signType", "md5");
		map.put("partner", partner);
		map.put("charset", "utf-8");
		map.put("version", "1.0");
		map.put("timestamp", DateUtils.currentDatetime());
		/*签名*/


		for (int i = 0 ;i < 20 ;i ++){
			map.put("times", String.valueOf(i));
			String sign = SignUtil.md5Sign(map, key, "utf-8");
			System.out.println(sign);
			map.put("sign", sign);
			HttpClient.sendRequestByPost(url, JsonUtil.objectToJson(map), true);
		}

	}

	@Test
	public void testRequest(){
		/*md5key*/
		def key="e06c27b7bfaf4e6196f1ac34ab3c1424"
		def data=[
			"signType":"md5"
			,"partner":"cd570067237847bda366d118a36140ac"
			,"service":"tcwindows.rent.callback"
			,"charset":"utf-8"
			,"timestamp": DateUtils.currentDatetime()
			,"version":"1.0"
			,"delCarNumList":"[{\"omRentGroupId\":\"e132a8a798464fd58c8baf198a4b8590\",\"carNum\":\"京A111111\"},{\"omRentGroupId\":\"e132a8a798464fd58c8baf198a4b8590\",\"carNum\":\"京A222222\"}]"
			,"pmParkId":"e132a8a798464fd58c8baf198a4b8590"
			,"omRentId":"e132a8a798464fd58c8baf198a4b8590"
			,"code":"0"
			,"desc":"12"
			,"notifyDesc":"321"
		]
		/*签名*/
		def sign=SignUtil.md5Sign(data,key,"utf-8")
		data.put("sign",sign)
		def url="http://localhost:8080/openapi/gateway"
		def result=HttpClient.sendRequestByPost(url,JsonUtil.objectToJson(data),true)
	}

	/**
	 * 测试三方调用
	 * 合作账号： 1c0984d344244fc2a4d2627875df4e25
	 * 商户名称： test_a
	 * 接口方法： tcserver.wallet.getCreditc测试
	 * 功能描述：	 信用额度
	 * 系统名称： 信用额度查询
	 * 负责人： 张欢
	 * 接口地址：http://test.tingjiandan.com/tcserver/gateway
	 * 参数模板：
	 {
	 "command": "wallet",
	 "method": "getCredit",
	 "userId": "${userId}",
	 "platform": "${platform}",
	 "version": "2.7.0",
	 "channel": "96"
	 }
	 * contentType：application/json
	 * 请求类型：POST
	 * @author youxin.zhou
	 * @date 2016年5月4日
	 */

	@Test
	public void testOpenApi(){
		/*md5key*/
		def key="faae7ad0b6d24df5bf709d588d6c636f"
		def param="{\"carNum\":\"青A11111\",\"charset\":\"utf-8\",\"freeMins\":\"\",\"partner\":\"748ebfb577844b8b83d168cc3bbdac71\",\"service\":\"tcroute.parkinginfo.orderInfo\",\"sign\":\"33AC7583394A0903A481999725643E62\",\"signType\":\"md5\",\"timestamp\":\"2016-07-12 17:49:51\",\"tradeId\":\"\",\"version\":\"1.0\"}"
		def data=JsonUtil.toMap(param)

		/*签名*/
		def sign=SignUtil.md5Sign(data,key,"utf-8")
		data.put("sign",sign)
//		def url="http://127.0.0.1:8080/openapi/gateway"
		def url="http://testapi.tingjiandan.com/openapi/gateway"
		println data
		def result=HttpClient.sendRequestByPost(url,JsonUtil.objectToJson(data),true)
		println result
	}


	@Test
	public void testOpenMgr(){
		/*md5key*/
		def key="510fdcb184264e39a330ed379e157bc0"
		def data=[
			"signType":"md5",
			"partner":"1c0984d344244fc2a4d2627875df4e25",
			"service":"mgr.park.getParkInfoList",
			"charset":"utf-8",
			"version":"1.0",

			"scope":"3000",
			"longitude":"116.445408",
			"latitude":"39.902434",
			"timestamp": DateUtils.currentDatetime()
		]
		/*签名*/
		def sign=SignUtil.md5Sign(data,key,"utf-8")
		data.put("sign",sign)
		def url="http://localhost:8080/openapi/gateway"
		def result=HttpClient.sendRequestByPost(url,JsonUtil.objectToJson(data),true)
		println result
	}

	@Test
	public void testOpenApiChinese(){
		/*md5key*/
		def key="510fdcb184264e39a330ed379e157bc0"
		def data=[
			"signType":"md5",
			"partner":"1c0984d344244fc2a4d2627875df4e25",
			"service":"groups.gcOut.selectCarGroupCarList",
			"charset":"utf-8",
			"version":"1.0",

			"service":"groups.gcOut.selectCarGroupCarList",
			"gcCargroupId":"3156514a7f2e433eb3f6cb3da0a18b7f",
			"carNum":"京L32499",
			"start":"0",
			"limit":"10",
			"timestamp": DateUtils.currentDatetime()
		]
		/*签名*/
		def sign=SignUtil.md5Sign(data,key,"utf-8")
		println "sign===================>" + sign
		data.put("sign",sign)
//		def url="http://testapi.tingjiandan.com/openapi/gateway"
		def url="http://localhost:8080/openapi/gateway"
		println data
		def result=HttpClient.sendRequestByPost(url,JsonUtil.objectToJson(data),true)
		println result
	}


	public static void main(String[] args) {
	}
}
